﻿
# 
def now():
	return "2015-09-21"

f=now
print "f=now"
print "f() =",f()

print "now.__name__ =",now.__name__
print "f.__name__ =",f.__name__

# wrapper()函数的参数定义是(*args, **kw)，因此，wrapper()函数可以接受任意参数的调用
def log(func):
	def wrapper(*args,**kw):
		print "call %s():" % func.__name__
		return func(*args,**kw)
	return wrapper

# 把@log放到now2()函数的定义处，相当于执行了语句：now = log(now2)
@log
def now2():
	return "2015-09-21"

print "now2() =",now2()

def log2(text):
	def decorator(func):
		def wrapper(*args,**kw):
			print "%s %s():" % (text,func.__name__)
			return func(*args,**kw)
		return wrapper
	return decorator

@log2('execute')
def now3():
	print "2015-09-21"

# 和两层嵌套的decorator相比，3层嵌套的效果是这样的：>>> now = log2('execute')(now3)
print "now3() =",now3()
print "now3.__name__ =",now3.__name__


import functools
def log3(text):
	def decorator(func):
		@functools.wrap(func)
		def wrapper(*args,**kw):
			print "%s %s():" % (text,func.__name__)
			return func(*args,**kw)
		return wrapper
	return decorator

@log3
def now4():
	print "2015-09-21 now4"

print "now4() =",now4
print "now4.__name__ =",now4.__name__